package com.health.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.common.common.ErrorCode;
import com.health.common.exception.BusinessException;
import com.health.common.model.vo.UserKeyVO;
import com.health.common.service.util.SDKUtils;
import com.health.user.mapper.HospitalMapper;
import com.health.user.model.dto.hospital.HospitalRegisterRequest;
import com.health.user.model.entity.Hospital;
import com.health.user.service.HospitalService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.v3.codec.ContractCodecException;
import org.fisco.bcos.sdk.v3.model.TransactionReceipt;
import org.fisco.bcos.sdk.v3.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.v3.transaction.model.exception.TransactionBaseException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.health.common.constant.ContractConstants.ADD_HOSPITAL;

/**
* @author zhexueqi
* @description 针对表【hospital(医院表)】的数据库操作Service实现
* @createDate 2024-08-25 01:31:10
*/
@Service
@Slf4j
public class HospitalServiceImpl extends ServiceImpl<HospitalMapper, Hospital>
    implements HospitalService {

    @Resource
    private SDKUtils sdkUtils;

//    @Value("${system.contract.contractName}")
//    private String CONTRACT_NAME;
//
//    @Value("${system.contract.address}")
//    private String CONTRACT_ADDRESS;
    @Value("${system.hospital.contract.contractName}")
    private String CONTRACT_NAME;

    @Value("${system.hospital.contract.address}")
    private String CONTRACT_ADDRESS;

    @Value("${system.hospital.contract.abi}")
    private String ABI;
    @Value("${system.hospital.contract.bin}")
    private String BIN;


    /**
     * 创建mysql医院用户
     */
    @Override
    public void createHospitalToMysql(HospitalRegisterRequest hospitalRegisterRequest) {
        if (getOne(new QueryWrapper<Hospital>().eq("hospital_account", hospitalRegisterRequest.getHospitalAccount())) != null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"医院账号已存在");
        }
        Hospital hospital  = new Hospital();
        hospital.setHospitalAccount(hospitalRegisterRequest.getHospitalAccount());
        hospital.setHospitalName(hospitalRegisterRequest.getHospitalName());
        hospital.setContactInfo(hospitalRegisterRequest.getContractInfo());
        hospital.setCoord(hospitalRegisterRequest.getCoord());
        if (!save(hospital)){
            throw new BusinessException(ErrorCode.SQL_ERROR,"注册医院失败");
        }
        log.info("创建mysql医院用户成功");
    }

    /**
     * 创建区块链医院用户
     * @param userKeyVO
     * @param userAccount
     */
    @Override
    public void createHospital(UserKeyVO userKeyVO, String userAccount) throws TransactionBaseException, ContractCodecException {
        QueryWrapper<Hospital> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hospital_account",userAccount);
        Hospital hospital = this.getOne(queryWrapper);
        if (hospital == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户不存在");
        }
        List<Object> funcParams = new ArrayList<>();
        funcParams.add(hospital.getHospitalName());
        funcParams.add(hospital.getCoord());
        funcParams.add(hospital.getContactInfo());
        funcParams.add(userKeyVO.getAddress());
        AssembleTransactionProcessor assembleTransactionProcessor = sdkUtils.transactionProcessor(CONTRACT_NAME,userKeyVO.getPrivateKey(),ABI,BIN);

        TransactionReceipt transactionReceipt = assembleTransactionProcessor.sendTransactionAndGetReceiptByContractLoader(CONTRACT_NAME, CONTRACT_ADDRESS, "addHospital1", funcParams);
        if (!transactionReceipt.isStatusOK()){
            throw new BusinessException(ErrorCode.CONTRACT_ERROR,"区块链注册失败");
        }
        log.info("区块链注册成功，交易哈希为：{}",transactionReceipt.getTransactionHash());
    }
}




